package com.model.proxy
{
	import com.ApplicationFacade;
	import com.model.proxy.RemoteDelegate;
	import com.model.vo.FormSaveVO;
	
	import mx.controls.Alert;
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	import org.puremvc.as3.multicore.utilities.statemachine.StateMachine;
	import org.puremvc.as3.multicore.interfaces.IProxy;
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;

	public class FormSaveProxy extends Proxy implements IProxy
	{
		public static const NAME:String='FormSaveProxy';

		private var remoteDelegate:RemoteDelegate;
		private var faultMessage:String;

		public var formVO:FormSaveVO=new FormSaveVO();

		public function FormSaveProxy()
		{
			super(NAME);
		}

		public function saveData(argumentList:Array):void
		{
			CursorManager.setBusyCursor();
			remoteDelegate=new RemoteDelegate(new Responder(onResult, onFault), formVO.remoteClass, formVO.remoteMethod, formVO.remoteEndpoint);
			var arg:Array=new Array();
			arg.push(argumentList);
			remoteDelegate.remoteArguments=arg;
			arg=null;
			remoteDelegate.sendData();
		}

		private function onResult(event:ResultEvent):void
		{
			CursorManager.removeBusyCursor();
			var res:Object=event.result;
			/*var resultArray:ArrayCollection=new ArrayCollection();
			   for (var i:String in res)
			   {
			   resultArray.addItem(res[i]);
			 }*/
			sendNotification(StateMachine.ACTION, res.toString(), ApplicationFacade.SAVED);
			res=null;
			//resultArray=null;
		}

		private function onFault(event:FaultEvent):void
		{
			CursorManager.removeBusyCursor();
			faultMessage=event.fault.faultString;
			sendNotification(StateMachine.ACTION, faultMessage, ApplicationFacade.FAILED);
		}
	}
}